﻿//+-------------------------------------------------------------------------------------------------------------------------------------------------------
//
//Description: OperationCodeHelper
//Copyright © Microsoft Corporation
//Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
//You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
//
//THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, 
//INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT. 
//See the Apache Version 2.0 License for specific language governing permissions and limitations under the License.
// 
//--------------------------------------------------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Xml.Linq;

namespace VmroleAuthor.ViewModels
{
    public class OperationCodeHelper
    {
        private const string OperationCodesXml = "OperationCodes.xml";
        private const string OperationDescription = "OperationDescription";
        private const string Code = "Code";
        private const string Description = "Description";

        public static Dictionary<int, string> OperationCodes;
        private static readonly object LockObject = new object();

        static OperationCodeHelper()
        {
            lock (LockObject)
            {
                if (OperationCodes == null)
                {
                    OperationCodes = new Dictionary<int,string>();
                    ConstructOperationCodeMapFromFile();
                }
            }
        }

        /// <summary>
        /// create a map of operation codes and descriptions
        /// </summary>
        private static void ConstructOperationCodeMapFromFile()
        {
            try
            {
                XDocument xDocument = XDocument.Load(OperationCodesXml);
                if (xDocument.Root != null)
                {
                    XNamespace xNamespace = xDocument.Root.Name.Namespace;

                    foreach (XElement element in xDocument.Root.Elements(xNamespace + OperationDescription))
                    {
                        var xElement = element.Element(xNamespace + Code);
                        if (xElement != null)
                        {
                            var xElement1 = element.Element(xNamespace + Description);
                            if (xElement1 != null)
                                OperationCodes.Add(Int32.Parse(xElement.Value), xElement1.Value);
                        }
                    }
                }
            }
            catch
            {
                // log
            }
        }

        /// <summary>
        /// get the description for the operations code
        /// </summary>
        /// <param name="status"></param>
        /// <returns></returns>
        public static string GetMessageForDisplay(SubStatus status)
        {
            var code = status.Code;
            // some codes have the parameterization flag set
            if (code > 0x00800000)
            {
                code = status.Code - 0x00800000;
            }

            if (OperationCodes.ContainsKey(code))
            {
                switch(status.Status)
                {
                    case HandlerStatusStates.Error:
                        return "Extension reported failure on operation : " + OperationCodes[code];
                       
                    case HandlerStatusStates.Transitioning:
                        return "Extension reported operation : " + OperationCodes[code] + " in progress";

                    case HandlerStatusStates.Warning:
                        return "Extension reported completion with warnings on operation : " + OperationCodes[status.Code];

                    case HandlerStatusStates.Success:
                        return "Extension reported success on operation : " + OperationCodes[code];
                }
                return "Extension reported code : " + status.Code;
            }
            return "Extension reported code : " + status.Code.ToString(CultureInfo.InvariantCulture);
        }
    }

}
